import Vue from 'vue'
import App from '@/App.vue'
import router from '@/router'
// 全局样式
import '@/assets/css/global.css'
// elementUi
import elementUi from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
// 字体图标样式
import '@/assets/fonts/iconfont.css'
import axios from 'axios'
// 全局样式
import Aside from '@/components/common/Aside.vue'
import Breadcrumb from '@/components/common/Breadcrumb.vue'
import Form from '@/components/common/Form.vue'
import Pagination from '@/components/common/Pagination.vue'
import Table from '@/components/common/Table.vue'
import Dialog from '@/components/common/Dialog.vue'
import Tabletree from 'vue-table-with-tree-grid'
import FromNew from '@/components/common/FormNew.vue'
// 富文本编辑器及其样式
import VueQuillEditor from 'vue-quill-editor'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { Message } from 'element-ui'
import dataV from '@jiaminghi/data-view'
import store from './store'
Vue.config.productionTip = false
// axios配置默认根路径
axios.defaults.baseURL = 'http://8.138.102.145/api/'
// axios请求拦截
axios.interceptors.request.use(config => {
  // 为请求头添加Authorization字段=token
  // 请求登录时为空
  NProgress.start()
  config.headers.Authorization = window.sessionStorage.getItem('token')
  return config
}, (error) => {
  // 对请求错误做些什么
  return Promise.reject(error)
})
axios.interceptors.response.use(config => {
  NProgress.done()
  return config.data
}, (error) => {
  // 从错误对象中获取响应
  const response = error.response;
  // 根据状态码进行处理
  if (response) {
    const { data: { status, msg } } = response;
    console.log(response);
    // 处理 500 内部服务器错误
    if (status === 500) {
      Message.error(msg);
    }
    // 处理 401 未授权错误
    if (status === 401) {
      Message.error('未授权，请登录');
      app.$router.push('/login')
    }

    // 处理 403 禁止访问错误
    if (status === 403) {
      Message.error('禁止访问');
    }

    // 处理 404 未找到错误
    if (status === 404) {
      Message.error('请求的资源不存在');
    }
  } else {
    // 处理其他错误，如网络错误等
    Message.error('网络错误，请稍后重试');
  }

  return Promise.reject(error);
})
Vue.prototype.$http = axios
Vue.use(elementUi)
Vue.use(dataV)
Vue.use(VueQuillEditor)
Vue.component('Aside', Aside)
Vue.component('Breadcrumb', Breadcrumb)
Vue.component('Form', Form)
Vue.component('Pagination', Pagination)
Vue.component('Table', Table)
Vue.component('Dialog', Dialog)
Vue.component('Tabletree', Tabletree)
Vue.component('FromNew', FromNew)
Vue.filter('dateFormat', function (value) {
  const pattern = ''
  var dt = new Date(value)
  var y = dt.getFullYear()
  var m = (dt.getMonth() + 1).toString().padStart(2, '0')
  var d = dt.getDate().toString().padStart(2, '0')
  if (pattern.toLowerCase() === 'yyyy-mm-dd') {
    return `${y}-${m}-${d}`
  } else {
    var hh = dt.getHours().toString().padStart(2, '0')
    var mm = dt.getMinutes().toString().padStart(2, '0')
    var ss = dt.getSeconds().toString().padStart(2, '0')
    return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
  }
})
const app=new Vue({
  router,
  store,
  render: h => h(App),
  beforeCreate() {
    Vue.prototype.$bus = this
  }
}).$mount('#app')

